package com.intellagric.service.traceability.Impl.trace_record;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.intellagric.common.pojo.LayuiDataGridResult;
import com.intellagric.common.pojo.ResponseMessage;
import com.intellagric.dao.traceability.RecordTransportDao;
import com.intellagric.mapper.TraceRecordTransportMapper;
import com.intellagric.module.traceability.RecordMachiningVo;
import com.intellagric.module.traceability.TransportRecordVo;
import com.intellagric.pojo.TraceRecordMachining;
import com.intellagric.pojo.TraceRecordTransport;
import com.intellagric.pojo.TraceRecordTransportExample;
import com.intellagric.service.traceability.trace_record.TransportrecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: zhy
 * @email: 1269679946@qq.com
 * @Date: 2019/1/12
 * @DESC： 运输物流记录服务
 */
@Service
public class TransportrecordServiceImpl implements TransportrecordService {

    @Autowired
    private TraceRecordTransportMapper recordTransportMapper;

    @Autowired
    private RecordTransportDao recordTransportDao;

    /**
     * 添加运输物流记录
     * @param recordTransport
     * @return ResponseMessage
     */
    @Override
    public ResponseMessage addTransportrecord(TraceRecordTransport recordTransport) {
        if (recordTransportMapper.insertSelective(recordTransport) == 1) {
            return ResponseMessage.success();
        } else {
            return ResponseMessage.fail();
        }
    }

    /**
     * 删除运输物流记录
     * @param id
     * @return ResponseMessage
     */
    @Override
    public ResponseMessage deleteITransportrecord(String id) {
        if (recordTransportMapper.deleteByPrimaryKey(id) == 1) {
            return ResponseMessage.success();
        } else {
            return ResponseMessage.fail();
        }
    }

    /**
     * 编辑运输物流记录
     * @param recordTransport
     * @return ResponseMessage
     */
    @Override
    public ResponseMessage editTransportrecord(TraceRecordTransport recordTransport) {
        if (recordTransportMapper.updateByPrimaryKeySelective(recordTransport) == 1 ) {
            return ResponseMessage.success();
        } else {
            return ResponseMessage.fail();
        }
    }

    /**
     * 通过id查询运输物流记录
     * @param id
     * @return TraceRecordTransport
     */
    @Override
    public TraceRecordTransport getTransportrecordById(String id) {
        return recordTransportMapper.selectByPrimaryKey(id);
    }

    /**
     * 查询运输物流记录列表
     * @return LayuiDataGridResult
     */
    @Override
    public LayuiDataGridResult getTransportrecordList() {
        TraceRecordTransportExample transportExample = new TraceRecordTransportExample();
        transportExample.setOrderByClause("create_date desc");
        List<TraceRecordTransport> recordTransportList = recordTransportMapper.selectByExample(transportExample);
        return LayuiDataGridResult.success().add(recordTransportList,recordTransportList.size());
    }

    /**
     * 分页查询运输物流记录列表
     * @return LayuiDataGridResult
     */
    @Override
    public LayuiDataGridResult getTransportrecordPage(int page, int rows) {
        PageHelper.startPage(page,rows);
        TraceRecordTransportExample transportExample = new TraceRecordTransportExample();
        transportExample.setOrderByClause("create_date desc");
        transportExample.createCriteria().andIdIsNotNull();
        List<TraceRecordTransport> recordTransportList = recordTransportMapper.selectByExample(transportExample);
        PageInfo<TraceRecordTransport> pageInfo = new PageInfo<>(recordTransportList);
        return LayuiDataGridResult.success().add(recordTransportList,pageInfo.getTotal());
    }

    /**
     * 多条件分页查询运输物流记录列表
     * @return LayuiDataGridResult
     */
    @Override
    public LayuiDataGridResult getTransportrecordPageByVo(int page, int rows, TransportRecordVo transportRecordVo) {
        PageHelper.startPage(page,rows);
        List<TraceRecordTransport> recordMachiningList = recordTransportDao.selectTransportrecordPageByVo(transportRecordVo);
        //获取分页信息
        PageInfo<TraceRecordTransport> pageInfo = new PageInfo<>(recordMachiningList);
        return LayuiDataGridResult.success().add(recordMachiningList,pageInfo.getTotal());
    }
}
